package com.data.service.biz;

import com.data.model.catalog.TableCatalogDTO;
import com.data.model.constant.SqlParamConstant;
import com.data.model.request.IndexQueryRequest;
import com.data.model.request.SimpleQueryRequest;
import com.data.model.request.SyncTableRequest;
import com.data.model.request.query.QueryTableRequest;
import com.data.service.build.QueryConstructionBuilder;
import com.data.service.build.SqlBuilderResult;
import com.data.service.build.TableStructureBuilder;
import com.data.service.catalog.CatalogService;
import com.data.service.datasource.DatasourceService;
import com.data.service.extension.DataSourceExtension;
import com.data.service.extension.Datasource;
import com.data.service.extension.DatasourceFactory;
import com.data.service.extension.DataSourceSwitcher;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DataSourceFacadeImpl implements DataSourceFacade {

    @Resource
    private DatasourceService datasourceService;
    @Resource
    private DatasourceFactory datasourceFactory;
    @Resource
    private CatalogService catalogService;

    @Override
    public List<Map<String, Object>> syncTable(SyncTableRequest request) {
        // 从配置中获取
        Datasource datasource = DataSourceSwitcher.getDataSource(request.getDatabaseName());

        String code = datasource.getTableInfoCode();

        Map<String, String> params = new HashMap<>();
        params.put(SqlParamConstant.SQL, buildShowTableSql(datasource.getType(), request));
        return datasourceService.query(code, params);
    }


    private String buildShowTableSql(String type, SyncTableRequest request) {
        DataSourceExtension dataSourceExtension = datasourceFactory.getDataSourceExtension(type);
        return dataSourceExtension.generateTableInfoSql(request.getDatabaseName(), request.getTableName());
    }

    @Override
    public List<Map<String, Object>> indexQuery(IndexQueryRequest request) {
        return Lists.newArrayList();
    }

    @Override
    public List<Map<String, Object>> simpleQuery(SimpleQueryRequest request) {
        String tableCode = request.getTableCode();

        QueryTableRequest queryTableRequest = new QueryTableRequest();
        queryTableRequest.setCode(tableCode);
        List<TableCatalogDTO> tableCatalogDTOS = catalogService.queryTable(queryTableRequest);

        TableCatalogDTO tableCatalogDTO = tableCatalogDTOS.get(0);

        SqlBuilderResult tableSql = TableStructureBuilder.execute(tableCatalogDTO, null);
        SqlBuilderResult sqlResult = QueryConstructionBuilder.execute(tableSql, request);
        String sql = sqlResult.getSql();

        Datasource datasource = DataSourceSwitcher.getDataSource("data");
        DataSourceExtension dataSourceExtension = datasourceFactory.getDataSourceExtension(datasource.getType());
        Map<String, String> params = new HashMap<>();
        params.put(SqlParamConstant.SQL, sql);
        return dataSourceExtension.execute(datasource, params);
    }
}
